<template>
    <rx-layout>
        <div slot="left" width="200" showButton="true">
            <rx-fit>
                <rx-category-treeeditor
                    cat-key="BPM"
                    :statisticConfig="statisticConfig"
                    read-key="start.read"
                    :edit="false"
                    @handSelect="handSelect"
                    v-if="isShowManageTree"
                    :isAdmin="isManageToTree"
                    :treeRootList="treeRootList"
                    :isBpmClient="isBpmClient"
                    :async="true">
                </rx-category-treeeditor>

            </rx-fit>
        </div>
        <div slot="center">
            <rx-fit :isheader="isheader">
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <rx-list-title :title="i18n('list.newProcess','新建流程')"></rx-list-title>
                    <span class="search-btn-box" v-if="!isheader">
                    <span class="search-btn" @click.stop="searchshow"><i class="iconfont iconguolv"></i>{{$ti18('rxList.leachBtn','过滤')}}</span>
                </span>
                </div>
                <div btnalign="right" slot="searchbar" v-if="fitSearch" @search="search" @cancel="cancel"
                     @close="closeSearchbar">
                    <a-form :layout="colLayout">
                        <a-row :gutter="24">
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.name','标题')">
                                    <a-input :placeholder="i18n('form.name','标题','input')"
                                             v-model="queryParam.Q_NAME__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.key','编码')">
                                    <a-input :placeholder="i18n('form.key','编码','input')"
                                             v-model="queryParam.Q_KEY__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.description','描述')">
                                    <a-input :placeholder="i18n('form.description','描述','input')"
                                             v-model="queryParam.Q_DESCP__S_LK"/>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <rx-grid
                    ref="table"
                    v-if="isShowManageTree"
                    bordered
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    url="/api-bpm/bpm/core/bpmDef/getAllForStart"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="['10','30','40']"
                    id-field="defId"
                    :loadDataOnstart="true"
                    @selectChange="onSelectChange"
                >
             <span slot="name" slot-scope="{ text, record }">
               <a-icon type="form"/>&nbsp;<span class="title" v-if="findAuthRight('def.start')"
                                                @click.stop="startFlow(record)">{{record.name}}</span>
                 <span v-else>{{record.name}}</span>
            </span>
                    <span slot="appId" slot-scope="{ text, record }">
                {{allApps[text]?$i18(allApps[text].appName,'应用名称'):$ti18('rxList.noAppName','无')}}
            </span>
            <span slot="action" slot-scope="{text, record}" class="actionIcons">
                <span v-if="findAuthRight('def.start')" @click.stop="startFlow(record)"> &nbsp;{{i18n('list.start','启动')}}</span>
                <span v-if="!collectList[record.defId]" @click.stop="setBpmCollect(record,true)">收藏</span>
                <span v-if="collectList[record.defId]" @click.stop="setBpmCollect(record,false)">取消收藏</span>
            </span>
                </rx-grid>
            </rx-fit>
        </div>
    </rx-layout>

</template>

<script>
    import BpmDefApi from '@/api/bpm/core/bpmDef'

    import RxCategoryTreeeditor from "@/components/redxun/rx-category-treeeditor";
    import {RxLayout, Util, RxFit, RxGrid, AppBaseList} from '@lowcode/jpaas-common-lib'
    import FlowUtil from "@/views/modules/bpm/js/FlowUtil.js";
    import {mapState} from 'vuex';
    import AppSearch from "@/views/modules/share/AppSearch";
    import RxSearchApp from "@/views/modules/share/rx-search-app";
    import instAndTaskShare from "@/views/modules/bpm/core/js/instAndTaskShare";
    export default {
        name: 'BpmDefForStart',
        mixins: [AppBaseList,instAndTaskShare, FlowUtil,AppSearch],
        components: {

            RxLayout,
            RxFit,
            RxGrid,
            RxCategoryTreeeditor,
            RxSearchApp
        },
        computed: {
            ...mapState({
                // 所属应用
                allApps: state => state.appSetting ? state.appSetting.allApps : {}
            }),
        },
        data() {
            return {
                // 查询参数
                queryParam: {},
                // 表头
                columns: [
                    {
                        title: this.$ti18('rxList.index', '序号'),
                        type: 'indexColumn',
                        dataIndex: 'serial',
                        scopedSlots: {customRender: 'serial'},
                        width: 50
                    },
                    {
                        title: this.i18n('form.name', '标题'),
                        dataIndex: 'name',
                        width: 240,
                        scopedSlots: {customRender: 'name'},
                    },
                    {
                        title: this.i18n('form.key', '编码'),
                        dataIndex: 'key',
                        width: 130
                    },
                    {
                        title: this.i18n('form.appId', '所属应用'),
                        dataIndex: 'appId',
                        width: 140,
                        sortField: 'APP_ID_',
                        scopedSlots: {customRender: 'appId'}
                    },
                    {
                        title: this.i18n('form.version', '版本号'),
                        dataIndex: 'version',
                        width: 120
                    },
                    {
                        title: this.$ti18('rxForm.createTime', '创建时间'),
                        dataIndex: 'createTime',
                        width: 200
                    },
                    {
                        title: this.$ti18('rxList.action', '操作'),
                        dataIndex: 'action',
                        scopedSlots: {customRender: 'action'},
                        width: 120
                    }
                ],
                curTree: {},
                statisticConfig: {
                    type: "static_bpm",
                    table: "BPM_DEF",
                    field: "TREE_ID_",
                    whereConf: [{name: "STATUS_", type: "string", dateFormat: "", op: "=", value: "DEPLOYED"},
                        {name: "IS_MAIN_", type: "string", dateFormat: "", op: "=", value: "YES"}
                    ]
                },
                //已收藏的流程
                collectList:{},
                isBpmClient:true,
                readKey:"start.read"
            }
        },
        created() {
            this.getBpmDefCollect()
        },
        methods: {
            i18n(name, text, key) {
                return this.$ti18(name, text, "BpmDefForStart", key);
            },
            handSelect(curRow) {
                this.curTree = curRow;
                var treeId = curRow.treeId;
                let param = {};
                this.queryParam['Q_APP_ID__S_EQ']="";
                this.queryParam['treeCatType']="";
                this.queryParam['treeRootList']="";
                if (treeId != "0") {
                    param = {Q_TREE_ID__S_EQ: treeId};
                    if("rootApp"!=curRow.treeCatType){
                        param['Q_APP_ID__S_EQ']=curRow.appId;
                    }
                } else {
                    param = {Q_TREE_ID__S_EQ: ''};
                    //顶级目录（包括平台与应用目录）
                    if("rootApp"!=curRow.treeCatType){
                        param['treeCatType']='allRootApp';
                        param['treeRootList']=JSON.stringify(this.getAppList());
                    }
                }
                if("rootApp"==curRow.treeCatType){
                    param = {
                        'Q_TREE_ID__S_EQ': '',
                        'treeCatType':'customApp',
                        'treeRootList':JSON.stringify(this.getAppList())
                    };
                    if("defaultTree"==curRow.appId){
                        //顶级平台目录
                        param['treeCatType']='defaultApp';
                    }else {
                        param['Q_APP_ID__S_EQ']=curRow.appId;
                    }
                }
                this.loadData(param);
            },
            //启动流程
            startFlow(record) {
                this.startProcess(record.key, record.appId);
            },
            setBpmCollect(record,isCollect){
                var self_=this
                BpmDefApi.setBpmCollect(record.defId,isCollect).then(res=>{
                    self_.getBpmDefCollect();
                    self_.loadData(self_.queryParam);
                })
            },
            getBpmDefCollect(){
                var self_=this;
                BpmDefApi.getBpmDefCollect().then(res=>{
                    self_.collectList={}
                    for(let data of res.data){
                        self_.collectList[data.defId]=true;
                    }
                    self_.loadData(self_.queryParam);
                })
            },
            search() {
                this.$refs.table.loadData()
            },
            closeSearchbar() {
                //关闭过滤弹窗；（点击X触发）
                this.fitSearch = false;
            }
        }
    }
</script>
<style scoped>

</style>
